﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BangumiWpfApp1
{
    public class BangumiApi
    {
        public event EventHandler<Subject> pull;
        public void download(int startId,int endId)
        {
            Stack<Subject> subjects = new Stack<Subject>();
            SubjectDao subjectDao = new SubjectDao();
            for (int i = startId; i <=endId; i++)
            {
                int subjectId = i;
                string url = $"https://api.bgm.tv/subject/{subjectId}?responseGroup=simple";
                var client = new RestClient(url);
                var request = new RestRequest(url, Method.GET);
                var response = client.Get(request);
                string resStr = response.Content;

                Subject subj = JsonConvert.DeserializeObject<Subject>(resStr);
                if (subj != null)
                {
                    Console.WriteLine($"{subj.type} {subj.id} {subj.name} {subj.name_cn} {subj.air_date}");
                    if (subj.rating != null)
                    {
                        subj.rate_total = subj.rating.total;
                        subj.score = subj.rating.score;
                    }
                    subjects.Push(subj);
                    //subjectDao.save(subj);
                    if (subj.type == 2)
                    {
                    }
                }
                else 
                {
                    Console.WriteLine($"{subjectId}");
                }
                pull?.Invoke(this,subj);


                List<Subject> save = new List<Subject>();
                int saveStep = 100;
                if (i == endId)
                {
                    int count = subjects.Count;
                    for (int k = 0; k < count; k++)
                    {
                        save.Add(subjects.Pop());
                    }
                    subjectDao.save(save);
                }
                else 
                {
                    if ((i - startId) % saveStep == 0)
                    {
                        int count = subjects.Count;
                        for (int k = 0; k < count; k++)
                        {
                            save.Add(subjects.Pop());
                        }
                        subjectDao.save(save);
                    }
                }
            }
        }

        internal void timeStatistics()
        {
            SubjectDao subjectDao = new SubjectDao();
            Dictionary<string, int> scoreDic = new Dictionary<string, int>();
            int totalStart = 1000;
            subjectDao.setRate_totalStart(totalStart);
            scoreDic.Add("[0,1890)", subjectDao.timeCount("0000-00-00", "1890-01-01"));
            scoreDic.Add("[1890,1950)", subjectDao.timeCount("1890-01-01", "1950-01-01"));
            scoreDic.Add("[1950,1970)", subjectDao.timeCount("1950-01-01", "1970-01-01"));
            scoreDic.Add("[1970,1990)", subjectDao.timeCount("1970-01-01", "1990-01-01"));
            scoreDic.Add("[1990,2000)", subjectDao.timeCount("1990-01-01", "2000-01-01"));
            scoreDic.Add("[2000,2005)", subjectDao.timeCount("2000-01-01", "2005-01-01"));
            scoreDic.Add("[2005,2006)", subjectDao.timeCount("2005-01-01", "2006-01-01"));
            scoreDic.Add("[2006,2007)", subjectDao.timeCount("2006-01-01", "2007-01-01"));
            scoreDic.Add("[2007,2008)", subjectDao.timeCount("2007-01-01", "2008-01-01"));
            scoreDic.Add("[2008,2009)", subjectDao.timeCount("2008-01-01", "2009-01-01"));
            scoreDic.Add("[2009,2010)", subjectDao.timeCount("2009-01-01", "2010-01-01"));
            scoreDic.Add("[2010,2011)", subjectDao.timeCount("2010-01-01", "2011-01-01"));         
            scoreDic.Add("[2011,2012)", subjectDao.timeCount("2011-01-01", "2012-01-01"));
            scoreDic.Add("[2012,2019)", subjectDao.timeCount("2012-01-01", "2013-01-01"));
            scoreDic.Add("[2013,2014)", subjectDao.timeCount("2013-01-01", "2014-01-01"));
            scoreDic.Add("[2014,2015)", subjectDao.timeCount("2014-01-01", "2015-01-01"));
            scoreDic.Add("[2015,2016)", subjectDao.timeCount("2015-01-01", "2016-01-01"));
            scoreDic.Add("[2016,2017)", subjectDao.timeCount("2016-01-01", "2017-01-01"));
            scoreDic.Add("[2017,2018)", subjectDao.timeCount("2017-01-01", "2018-01-01"));
            scoreDic.Add("[2018,2019)", subjectDao.timeCount("2018-01-01", "2019-01-01"));
            scoreDic.Add("[2019,2020)", subjectDao.timeCount("2019-01-01", "2020-01-01"));
            scoreDic.Add("[2020,2021)", subjectDao.timeCount("2020-01-01", "2021-01-01"));
            scoreDic.Add("[2021,2025)", subjectDao.timeCount("2021-01-01", "2025-01-01"));
            scoreDic.Add("[0,2025)", subjectDao.timeCount("0000-00-00", "2025-01-01"));
            Console.WriteLine($"time Statistics");
            foreach (var item in scoreDic)
            {
                Console.WriteLine($"{item.Key}  {item.Value}");
            }
        }

        internal void rateTotalStatisticsSimple()
        {
            SubjectDao subjectDao = new SubjectDao();
            Dictionary<string, int> scoreDic = new Dictionary<string, int>();
            scoreDic.Add("[0,1)", subjectDao.rateTotalCount(0, 1));
            scoreDic.Add("[1,100)", subjectDao.rateTotalCount(1, 100));
            scoreDic.Add("[100,200)", subjectDao.rateTotalCount(100, 200));
            scoreDic.Add("[200,500)", subjectDao.rateTotalCount(200, 500));
            scoreDic.Add("[500,1000)", subjectDao.rateTotalCount(500, 1000));
            scoreDic.Add("[1000,2000)", subjectDao.rateTotalCount(1000, 2000));
            scoreDic.Add("[2000,4000)", subjectDao.rateTotalCount(20000, 4000));
            scoreDic.Add("[4000,6000)", subjectDao.rateTotalCount(4000, 6000));
            scoreDic.Add("[6000,8000)", subjectDao.rateTotalCount(6000, 8000));
            scoreDic.Add("[8000,10000)", subjectDao.rateTotalCount(8000, 10000));
            scoreDic.Add("[10000,12000)", subjectDao.rateTotalCount(10000, 12000));
            scoreDic.Add("[12000,19000)", subjectDao.rateTotalCount(12000, 19000));
            scoreDic.Add("[0,19000)", subjectDao.rateTotalCount(0, 19000));
            scoreDic.Add("[300,19000)", subjectDao.rateTotalCount(300, 19000));
            scoreDic.Add("[1000,19000)", subjectDao.rateTotalCount(1000, 19000));
            Console.WriteLine($"rate_total Statistics");
            foreach (var item in scoreDic)
            {
                Console.WriteLine($"{item.Key}  {item.Value}");
            }
        }
        internal Dictionary<string, int> rateTotalStatistics()
        {
            SubjectDao subjectDao = new SubjectDao();
            Dictionary<string, int> scoreDic = new Dictionary<string, int>();
            scoreDic.Add("[0,2000)", subjectDao.rateTotalCount(0, 2000));         
            scoreDic.Add("[2000,4000)", subjectDao.rateTotalCount(20000, 4000));
            scoreDic.Add("[4000,6000)", subjectDao.rateTotalCount(4000, 6000));
            scoreDic.Add("[6000,8000)", subjectDao.rateTotalCount(6000, 8000));
            scoreDic.Add("[8000,10000)", subjectDao.rateTotalCount(8000, 10000));
            scoreDic.Add("[10000,12000)", subjectDao.rateTotalCount(10000, 12000));
            scoreDic.Add("[12000,14000)", subjectDao.rateTotalCount(12000, 14000));
            scoreDic.Add("[14000,16000)", subjectDao.rateTotalCount(14000, 16000));
            scoreDic.Add("[16000,18000)", subjectDao.rateTotalCount(16000, 18000));
            scoreDic.Add("[18000,20000)", subjectDao.rateTotalCount(18000, 20000));
            Console.WriteLine($"rate_total Statistics");
            foreach (var item in scoreDic)
            {
                Console.WriteLine($"{item.Key}  {item.Value}");
            }
            return scoreDic;
        }

        internal Dictionary<string, int> scoreStatisticsSimple()
        {
            SubjectDao subjectDao = new SubjectDao();
            Dictionary<string, int> scoreDic = new Dictionary<string, int>();
            int totalStart = 1000;
            subjectDao.setRate_totalStart(totalStart);
            scoreDic.Add("[0,1)", subjectDao.scoreCount(0, 1));
            scoreDic.Add("[1,2)", subjectDao.scoreCount(1, 2));
            scoreDic.Add("[2,3)", subjectDao.scoreCount(2, 3));
            scoreDic.Add("[3,4)", subjectDao.scoreCount(3, 4));
            scoreDic.Add("[4,5)", subjectDao.scoreCount(4, 5));
            scoreDic.Add("[5,6)", subjectDao.scoreCount(5, 6));
            scoreDic.Add("[6,7)", subjectDao.scoreCount(6, 7));
            scoreDic.Add("[7,8)", subjectDao.scoreCount(7, 8));
            scoreDic.Add("[8,9)", subjectDao.scoreCount(8, 9));
            scoreDic.Add("[9,10)", subjectDao.scoreCount(9, 10));
            scoreDic.Add("[0,10)", subjectDao.scoreCount(0, 10));
            Console.WriteLine($"score Statistics ,rate_total > {totalStart}");
            foreach (var item in scoreDic)
            {
                Console.WriteLine($"{item.Key}  {item.Value}");
            }
            return scoreDic;
        }
        internal Dictionary<string, int> scoreStatistics()
        {
            SubjectDao subjectDao = new SubjectDao();
            Dictionary<string, int> scoreDic = new Dictionary<string, int>();
            int totalStart = 0;
            subjectDao.setRate_totalStart(totalStart);
            scoreDic.Add("[0,1)", subjectDao.scoreCount(0,1));
            scoreDic.Add("[1,2)", subjectDao.scoreCount(1, 2));
            scoreDic.Add("[2,3)", subjectDao.scoreCount(2, 3));
            scoreDic.Add("[3,4)", subjectDao.scoreCount(3, 4));
            scoreDic.Add("[4,5)", subjectDao.scoreCount(4, 5));
            scoreDic.Add("[5,6)", subjectDao.scoreCount(5, 6));
            scoreDic.Add("[6,7)", subjectDao.scoreCount(6, 7));
            scoreDic.Add("[7,8)", subjectDao.scoreCount(7, 8));
            scoreDic.Add("[8,9)", subjectDao.scoreCount(8, 9));
            scoreDic.Add("[9,10)", subjectDao.scoreCount(9, 10));         
            Console.WriteLine($"score Statistics ,rate_total > {totalStart}");
            foreach (var item in scoreDic)
            {
                Console.WriteLine($"{item.Key}  {item.Value}");
            }
            return scoreDic;
        }

    }
}
